package com.cloudtasks;

import android.content.Context;
import android.os.AsyncTask;

import com.appspot.api.services.taskservice.Taskservice;
import com.appspot.api.services.taskservice.model.Task;
import com.google.api.client.extensions.android2.AndroidHttp;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.json.jackson.JacksonFactory;

import java.io.IOException;

public class AddTaskTask extends AsyncTask<Task, Void, Void> {

    private TaskAdapter adapter;
    private Context mContext;

    public AddTaskTask(TaskAdapter adapter, Context context) {
        this.adapter = adapter;
        this.mContext = context;
    }
    
    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        adapter.notifyDataSetChanged();
    }

    @Override
    protected Void doInBackground(Task... params) {
        
        if (params != null) {
            Task newTask = params[0];
            System.out.println(newTask.getNote());
            System.out.println(newTask.toString());
            String baseUrl = Util.getBaseUrl(mContext) + "_ah/api/a5463920/v1/";
            Taskservice taskService = Taskservice.builder(
                AndroidHttp.newCompatibleTransport(), 
                    new JacksonFactory())
                    .setBaseUrl(new GenericUrl(baseUrl))
                    .build();
            try {
                taskService.insertTask(newTask).execute();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        return null;
    }
    
}